var order="order by SALES_VOLUME desc",where="";
$(function(){
    getAllGood();
    $('#bySale').click(function(){
       if($(this).hasClass("current_tab_li")){
           if($(this).find(".current_tab_arrow_up").length>0){
               $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
               $(this).find("em:eq(1)").addClass('current_tab_arrow_down');
               order="order by SALES_VOLUME desc";
           }else{
               $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
               $(this).find("em:eq(0)").addClass('current_tab_arrow_up');
               order="order by SALES_VOLUME asc";
           }
           where = "";
       }else{
           $('.current_tab_li').removeClass('current_tab_li');
           $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
           $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
           $(this).addClass("current_tab_li");
           $(this).find("em:eq(1)").addClass('current_tab_arrow_down');
           order="order by SALES_VOLUME desc";
           where = "";
       }
        getAllGood();
    });

    $('#byStock').click(function(){
        if($(this).hasClass("current_tab_li")){
            if($(this).find(".current_tab_arrow_up").length>0){
                $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
                $(this).find("em:eq(1)").addClass('current_tab_arrow_down');
                order="order by stock desc";
            }else{
                $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
                $(this).find("em:eq(0)").addClass('current_tab_arrow_up');
                order="order by stock asc";
            }
            where = "";
        }else{
            $('.current_tab_li').removeClass('current_tab_li');
            $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
            $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
            $(this).addClass("current_tab_li");
            $(this).find("em:eq(1)").addClass('current_tab_arrow_down');
            order="order by stock desc";
            where = "";
        }
        getAllGood();
    });

    $("#byAllSaleed").click(function(){
        if($(this).hasClass("current_tab_li"))
            return;
        else{
            $('.current_tab_li').removeClass('current_tab_li');
            $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
            $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
            $(this).addClass("current_tab_li");
            order="";
            where = "where stock=0";
            getAllGood();
        }
    });

    $("#byIsShelvesd").click(function(){
        if($(this).hasClass("current_tab_li"))
            return;
        else{
            $('.current_tab_li').removeClass('current_tab_li');
            $('.current_tab_arrow_up').removeClass('current_tab_arrow_up');
            $('.current_tab_arrow_down').removeClass('current_tab_arrow_down');
            $(this).addClass("current_tab_li");
            order="";
            where = "where IS_SHELVES=0";
            getAllGood();
        }
    });

    $("#addGoodBtn").click(function(){
        getPage($("#addGoodPageA")[0],ctx+'/good/addGoodPage.do');
    });


});

function getAllGood(){
    $('#manageGoodList>li').each(function(index){
       if(index>1)
        $(this).remove();
       if(index==1)
        $(this).show();
    });
    $.ajax({
        url:ctx+"/good/list.do",
        data:{order:order,where:where},
        success: function (data) {
            var shangjia;
            for(var i=0;i<data.length;i++){
                if(data[i].isShelves)
                    shangjia = '<a class="down_item blu_txt block" onclick="xiajia('+data[i].id+')">下架商品</a>';
                else
                    shangjia = '<a class="down_item blu_txt block" onclick="shangjia('+data[i].id+')">上架商品</a>';
                $('#manageGoodList').append(
                ' <li class="list_li c_txt">' +
                '<div class="list_item_a">' +
                '<a href="#" target="_blank" class="list_i_a l_txt rel">' +
                '<img' +
                ' src="/resource/download.do?id=' + data[i].photo +'"'+
                'width="80" height="80" class="list_i_i">' +
                '<p class="list_i_p blu_txt over_hidden">'+data[i].name+'</p></a>' +
                '</div>' +
                '<div class="list_item_b">'+data[i].price+'</div>' +
                '<div class="list_item_c">'+data[i].salesVolume+'</div>' +
                '<div class="list_item_d">'+data[i].stock+'</div>' +
                '<div class="list_item_j">' +
                '<div class="cate" style="line-height: 22px;"></div>' +
                '</div>' +
                '<div class="list_item_f">' +
                shangjia +
                '<a' +
                ' class="del_item red_txt block"  onclick="deleteGood('+data[i].id+')">删除商品' +
                '</a>' +
                '</div>' +
                '<div class="clear"></div>' +
                '</li>');
            }
            $('#manageGoodList>li:eq(1)').hide();
        }
    })

}

function shangjia(id){
    $.ajax({
        url:ctx+"/good/update.do",
        data:{id:id,isShelves:1},
        success: function (data) {
            getAllGood();
        }
    });
}

function deleteGood(id){
    $.ajax({
        url:ctx+"/good/delete.do",
        data:{id:id},
        success: function (data) {
            getAllGood();
        }
    });
}

function xiajia(id){
    $.ajax({
        url:ctx+"/good/update.do",
        data:{id:id,isShelves:0},
        success: function (data) {
            getAllGood();
        }
    });
}